/*
 * i18n.java
 *
 * Created on 4 mars 2007, 00:37
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package blbarc;
import java.text.MessageFormat;
import java.util.*;

/**
 *
 * @author alexis
 */
public class i18n {
   private ResourceBundle messages;
   private Locale currentLocale;
   
   /**
    * Get the Configuration instance
    * @return Instancied configuration object
    */
   public static i18n getInstance() {
       if (instance == null) {
           instance = new i18n();
       }
       return instance;
   }

   private i18n() {
       this.reloadMessages();
   }
   
   public void reloadMessages() {
       String localeTxt = Configuration.getInstance().getLanguage();
       if (localeTxt.contains("_"))
           currentLocale = new Locale(localeTxt.split("_")[0], localeTxt.split("_")[1]);
       else
           currentLocale = new Locale(localeTxt);
       messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
   }
   
   public String getString(String name) {
       try {
           return messages.getString(name);
       } catch (java.util.MissingResourceException ex) {
           return name;
       }
   }
   
   public String getCompound(String name, Object[] compound) {
       MessageFormat formatter = new MessageFormat("");
       formatter.setLocale(currentLocale);

       formatter.applyPattern(messages.getString(name));
       return formatter.format(compound);
   }
   
   private static i18n instance;
}

